﻿/***************************************************************
 * 开发人员：网魂小兵@http://www.mozlite.com
 * 
 *  数据库提供者基类。
 * 
 * 最后修改：2011-10-31
 * ************************************************************/
namespace Mozlite.Common
{
    using System;
    using System.Collections.Generic;
    using System.Linq;
    using System.Text;
    using Mozlite.Data;
    using System.Data.Common;
    using Mozlite.Common.Configuration;

    /// <summary>
    /// 数据库提供者基类。
    /// </summary>
    public abstract class CommonDataProvider : ProviderBase<CommonDataProvider>
    {
        /// <summary>
        /// 初始化类<see cref="T:Mozlite.Common.CommonDataProvider"/>。
        /// </summary>
        /// <param name="factory">数据库提供者工厂实例。</param>
        protected CommonDataProvider(DbProviderFactory factory) : base(factory) { }

        #region applications
        /// <summary>
        /// 创建应用程序。
        /// </summary>
        /// <typeparam name="TApplication">应用程序类。</typeparam>
        /// <typeparam name="TConfiguration">应用程序配置类型。</typeparam>
        /// <param name="application">应用程序</param>
        /// <returns>返回执行结果。</returns>
        public abstract bool SaveApplication<TApplication, TConfiguration>(TApplication application)
            where TApplication : ApplicationBase<TApplication, TConfiguration>, new()
            where TConfiguration : ConfigurationBase<TConfiguration>, new();

        /// <summary>
        /// 删除应用程序。
        /// </summary>
        /// <param name="applicationID">应用程序唯一ID。</param>
        /// <returns>返回执行结果。</returns>
        public abstract bool DeleteApplication(string applicationID);

        /// <summary>
        /// 加载应用程序列表。
        /// </summary>
        /// <returns>返回应用程序列表。</returns>
        public abstract Dictionary<string, ApplicationBase> LoadApplications();

        
        /// <summary>
        /// 获取应用程序类型。
        /// </summary>
        /// <typeparam name="TApplication">应用程序类。</typeparam>
        /// <typeparam name="TConfiguration">应用程序配置类型。</typeparam>
        /// <param name="applicationID">应用程序唯一ID。</param>
        /// <returns>返回应用程序类型。</returns>
        public abstract TApplication GetApplication<TApplication, TConfiguration>(string applicationID)
            where TApplication : ApplicationBase<TApplication, TConfiguration>, new()
            where TConfiguration : ConfigurationBase<TConfiguration>, new();

        /// <summary>
        /// 加载区域列表。
        /// </summary>
        /// <returns>返回区域列表。</returns>
        public abstract List<string> LoadAreas();

        /// <summary>
        /// 保存应用程序数据。
        /// </summary>
        /// <typeparam name="TConfiguration">当前应用程序类型。</typeparam>
        /// <param name="configuration">应用程序实例对象。</param>
        /// <returns>返回是否执行成功。</returns>
        public abstract bool SaveConfiguration<TConfiguration>(TConfiguration configuration)
            where TConfiguration : ConfigurationBase<TConfiguration>, new();

        /// <summary>
        /// 获取应用程序数据配置实例。
        /// </summary>
        /// <param name="applicationID">应用程序ID。</param>
        /// <param name="settingsID">网站设置ID。</param>
        /// <returns>返回应用程序配置实例。</returns>
        public abstract ConfigurationBase GetConfiguration(string applicationID, int settingsID);
        #endregion

        #region logs
        /// <summary>
        /// 删除事件日志。
        /// </summary>
        /// <param name="settingsID">网站ID。</param>
        /// <param name="logIDs">事件日志ID集合，以","分割。</param>
        /// <returns>返回删除结果。</returns>
        public abstract bool DeleteLogs(int settingsID, string logIDs);
        /// <summary>
        /// 移除过期日期的日志。
        /// </summary>
        /// <param name="settingsID">网站ID。</param>
        /// <param name="date">过期时间。</param>
        public abstract void ClearLogs(int settingsID, DateTime date);
        /// <summary>
        /// 写入日志。
        /// </summary>
        /// <param name="info">当前日志实体类。</param>
        public abstract void WriteLog(LogInfo info);
        /// <summary>
        /// 分页加载事件日志。
        /// </summary>
        /// <param name="query">日志查询实例。</param>
        /// <param name="totalSize">返回所有记录数。</param>
        /// <returns>返回当前页码的记录事件日志。</returns>
        public abstract List<LogInfo> LoadLogs(LogQuery query, out int totalSize);
        /// <summary>
        /// 通过ID获取当前日志信息。
        /// </summary>
        /// <param name="logID">当前日志编号。</param>
        /// <returns>返回当前日志信息。</returns>
        public abstract LogInfo GetLog(int logID);
        #endregion

        #region mails
        /// <summary>
        /// 删除队列中的电子邮件。
        /// </summary>
        /// <param name="emailID">ID。</param>
        public abstract void DeleteQueuedMail(Guid emailID);
        /// <summary>
        /// 抽取Mail队列中的邮件。
        /// </summary>
        /// <param name="messageLimit">抽取数量。</param>
        /// <returns>返回抽取的电子邮件列表。</returns>
        public abstract Dictionary<Guid, Mail> DequeueMails(int messageLimit);
        /// <summary>
        /// 将电子邮件插入到队列中。
        /// </summary>
        /// <param name="message">电子邮件实例对象。</param>
        public abstract void QueueMail(Mail message);
        /// <summary>
        /// 将发送失败的对象插入到队列中。
        /// </summary>
        /// <param name="list">发送失败的电子邮件的ID列表。</param>
        /// <param name="failureInterval">几分钟后继续发送。</param>
        /// <param name="maxNumberOfTries">最大发送次数。</param>
        public abstract void QueueSendingFailure(List<Guid> list, int failureInterval, int maxNumberOfTries);
        #endregion

        #region categories
        /// <summary>
        /// 新建分类。
        /// </summary>
        /// <param name="category">分类实例对象。</param>
        /// <returns>返回创建分类状态。</returns>
        public abstract CreateCategoryStatus SaveCategory(Category category);

        /// <summary>
        /// 获取分类列表，字典键为分类ID。
        /// </summary>
        /// <param name="settingsID">网站设置ID。</param>
        /// <returns>返回分类列表。</returns>
        public abstract Dictionary<int, Category> LoadCategories(int settingsID);

        /// <summary>
        /// 获取分类。
        /// </summary>
        /// <param name="categoryID">分类ID。</param>
        /// <param name="settingsID">网站设置ID。</param>
        /// <returns>返回分类实例对象。</returns>
        public abstract Category GetCategory(int categoryID, int settingsID);

        /// <summary>
        /// 删除类型。
        /// </summary>
        /// <param name="categoryID">分类ID。</param>
        /// <param name="settingsID">网站设置ID。</param>
        /// <returns>返回删除结果。</returns>
        public abstract bool DeleteCategory(int categoryID, int settingsID);

        /// <summary>
        /// 计算所有分类文章数。
        /// </summary>
        public abstract void CalculateThreadsCount();
        #endregion

        #region threads
        /// <summary>
        /// 保存主题。
        /// </summary>
        /// <param name="thread">主题实例对象。</param>
        /// <returns>返回创建主题状态。</returns>
        public abstract CreateThreadStatus SaveThread(Thread thread);

        /// <summary>
        /// 获取主题列表。
        /// </summary>
        /// <param name="pageIndex">页码。</param>
        /// <param name="pageSize">每页显示的记录数。</param>
        /// <param name="settingsID">网站设置ID。</param>
        /// <param name="tagName">标签名称。</param>
        /// <param name="size">总记录数。</param>
        /// <returns>返回主题列表。</returns>
        public abstract List<Thread> GetTreadByTag(int pageIndex, int pageSize, int settingsID, string tagName, out int size);

        /// <summary>
        /// 获取主题列表。
        /// </summary>
        /// <param name="query">主题查询实例。</param>
        /// <param name="size">返回总记录数。</param>
        /// <returns>返回主题列表。</returns>
        public abstract List<Thread> GetThreads(ThreadQuery query, out int size);

        /// <summary>
        /// 获取主题实例。
        /// </summary>
        /// <param name="threadID">主题ID。</param>
        /// <param name="key">唯一键。</param>
        /// <param name="addViews">是否添加访问数量。</param>
        /// <param name="settingsID">网站设置ID。</param>
        /// <returns>返回主题实例对象。</returns>
        public abstract Thread GetThread(int threadID, string key, bool addViews, int settingsID);

        /// <summary>
        /// 删除主题。
        /// </summary>
        /// <param name="threadID">主题ID。</param>
        /// <param name="settingsID">网站设置ID。</param>
        /// <returns>返回删除结果。</returns>
        public abstract bool DeleteThreads(int threadID, int settingsID);
        #endregion

        #region tags
        /// <summary>
        /// 更新标签。
        /// </summary>
        /// <param name="tag">标签实例。</param>
        /// <returns>返回执行结果。</returns>
        public abstract bool SaveTag(Tag tag);

        /// <summary>
        /// 加载标签。
        /// </summary>
        /// <param name="settingsID">网站设置ID。</param>
        /// <param name="userID">用户ID。</param>
        /// <returns>返回标签列表。</returns>
        public abstract List<Tag> LoadTags(int settingsID, int userID);
        #endregion

        #region posts
        /// <summary>
        /// 保存帖子。
        /// </summary>
        /// <param name="post">帖子实例。</param>
        /// <returns>返回执行结果。</returns>
        public abstract bool SavePost(Post post);

        /// <summary>
        /// 载入帖子。
        /// </summary>
        /// <param name="pageIndex">页码。</param>
        /// <param name="pageSize">每页显示记录数。</param>
        /// <param name="threadID">主题ID。</param>
        /// <param name="settingsID">网站设置ID。</param>
        /// <param name="userid">用户ID。</param>
        /// <param name="size">总记录数。</param>
        /// <returns>返回帖子列表。</returns>
        public abstract List<Post> LoadPosts(int pageIndex, int pageSize, int? threadID, int settingsID, int? userid, out int size);

        /// <summary>
        /// 删除帖子。
        /// </summary>
        /// <param name="postID">帖子ID。</param>
        /// <param name="settingsID">网站设置ID。</param>
        /// <returns>返回删除结果。</returns>
        public abstract bool DeletePost(int postID, int settingsID);
        #endregion

        #region Navigators
        /// <summary>
        /// 保存导航实例。
        /// </summary>
        /// <param name="navigator">导航实例对象。</param>
        /// <returns>返回保存结果。</returns>
        public abstract bool SaveNavigator(Navigator navigator);
        
        /// <summary>
        /// 加载导航列表。
        /// </summary>
        /// <param name="settingsID">网站设置。</param>
        /// <param name="themeID">模板ID。</param>
        /// <returns>返回导航列表。</returns>
        public abstract Dictionary<NavigatorLocation, Dictionary<int, Navigator>> LoadNavigators(int settingsID, Guid themeID);

        /// <summary>
        /// 获取导航实例对象。
        /// </summary>
        /// <param name="settingsID">网站设置。</param>
        /// <param name="navigatorID">导航ID。</param>
        /// <returns>返回导航实例对象。</returns>
        public abstract Navigator GetNavigator(int settingsID, int navigatorID);
        #endregion

        #region content
        /// <summary>
        /// 保存内容。
        /// </summary>
        /// <param name="content">内容实例对象。</param>
        /// <returns>返回保存结果。</returns>
        public abstract bool SaveContent(ContentContainer content);

        public abstract ContentContainer GetContent(int contentID, string key, int settingsID, Guid themeID);

        public abstract string GetContentBody(string key, int settingsID);

        public abstract bool DeleteContent(int contentID, int settingsID);
        #endregion
    }
}
